package com.hqyj.javaSpringBoot.modules.test.controller;

import com.hqyj.javaSpringBoot.modules.common.entity.Result;
import com.hqyj.javaSpringBoot.modules.common.entity.SearchBean;
import com.hqyj.javaSpringBoot.modules.test.entity.Student;
import com.hqyj.javaSpringBoot.modules.test.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api")
public class StudentController {
    @Autowired
    private StudentService studentService;

    /**
     * 127.0.0.1:8081/api/student---------post
     * {"name":"chenlin","email":"232@qq.com","age":"18"}
     * {"name":"chenlin","email":"232@qq.com","age":"18","studentCord":{"cardNo":"0001"}}
     * @param student
     * @return
     */
    @PostMapping(value = "/student",consumes = "application/json")
    public Result<Student> insertStudent(@RequestBody Student student) {
//        return studentService.editStudent(student);
        return studentService.editStudent(student);
    }

    /**
     * 127.0.0.1:8081/api/student---------put
     */
    @PutMapping(value = "/student",consumes = "application/json")
    public Result<Student> updateStudent(@RequestBody Student student) {
        return studentService.editStudent(student);
    }

    /**
     * 127.0.0.1:8081/api/student/1---------delete
     * @param id
     * @return
     */
    @DeleteMapping(value = "/student/{id}")
    public Result<Object> deleteStudent(@PathVariable int id) {
        return studentService.deleteStudentById(id);
    }

    /**
     * 127.0.0.1:8081/api/student/1---------get
     * @param id
     * @return
     */
    @GetMapping(value = "/student/{id}")
    public Student getStudentById(@PathVariable int id) {
        return studentService.getStudentById(id);
    }

    /**
     * 127.0.0.1:8081/api/students---------get
     * @return
     */
    @GetMapping(value = "students")
    public List<Student> getStudents() {
        return studentService.getStudents();
    }

    /**
     * 127.0.0.1:8081/api/student?name=chenlin&email=232@qq.com---------get
     * @param name
     * @param email
     * @return
     */
    @GetMapping("/student")
    public Student getStudentByNameAndEmail(@RequestParam String name,
                                            @RequestParam String email) {
        return studentService.getStudentByNameAndEmail(name, email);
    }

    /**
     * 127.0.0.1:8081/api/students----------post
     *
     * {"currentPage":1,"pageSize":5,"orderBy":"name","direction":"desc","keyWord":""}
     * @param searchBean
     * @return
     */
    @PostMapping(value = "/students",consumes = "application/json")
    public Page<Student> getStudentsBySearchBean(@RequestBody SearchBean searchBean) {
        return studentService.getStudentsBySearchBean(searchBean);
    }
}
